package com.logger.common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 文件工具类
 * Created by chenxunlin01 on 2016/1/19.
 */

public class FilePicker {

    /**
     * 获取文件的输出流
     */
    public OutputStream getOutputStream(String path, boolean append) throws IOException {
        File file = checkExistFile(path, append);
        if (!file.exists()) {
            file.mkdirs();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file, append);
        } catch (FileNotFoundException e) {
            throw e;
        }
        return fos;
    }

    /**
     * 判断文件是否存在
     */
    protected File checkExistFile(String path, boolean append) {
        File file = new File(path);
        if (append) {
            return file;
        }
        int index = 1;
        while (file.exists()) {
            path += "." + (index++);
            file = new File(path);
        }
        return file;
    }
}
